﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AMG.Model;
using AMG.BLL.User;
using AMG.Common;
using AMG.Config;
using AMG.DALProfile;
namespace AMG.Web
{
    public partial class Login : AMG.Web.UI.Page.BasePage
    {
        protected string email = string.Empty;
        protected int flag = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["username"] != "29" && Request .Form ["username"]!=null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    if (Request.Form["UserName"] == null || Request.Form["Password"] == null
                        || Request.Form["UserName"].Trim() == string.Empty || Request.Form["Password"].Trim() == string.Empty)
                    {
                        flag = -2;
                        if (Request.QueryString["a"] == "relog")
                            flag = -2;
                    }
                    else
                    {
                        email = Request.Form["UserName"].Trim();
                        string Password = Request.Form["Password"].Trim().ToLower();
                        BLL.User.User us = new AMG.BLL.User.User();
                        string userName = string.Empty;
                        string userPortrait = string.Empty;
                        EnumLoginState state = us.Login(email, Password);
                        switch (state)
                        {
                            case EnumLoginState.Succeed:
                                //这里开始整合DZ NT登录

                                AMG.Web.UI.Page.UserPage.SetCookie(int.Parse(email), Input.MD5(Password, false));
                                if (email == "29")
                                {
                                    Response.Redirect("/admin/main.aspx");
                                }
                                else
                                {
                                    Response.Redirect("/main.aspx");
                                }
                                //Response.Write("<script language=\"javascript\">window.top.location.href='default.aspx';</script>");                                                       
                                break;
                            case EnumLoginState.Err_UnActivation:
                                flag = 1;
                                break;
                            case EnumLoginState.Err_NameOrPwdError:
                                flag = 0;
                                break;
                            case EnumLoginState.Err_Locked:
                                Response.Redirect("/ErrorPage.aspx?r=对不起，您的帐号已经被系统锁定，如有疑问，请与管理员联系");
                                break;
                        }
                    }
                    DataBind();
                }
            }
        }
    }
}